package bingo.codeweaver.core.metadata;

import java.util.ArrayList;
import java.util.List;

import bingo.codeweaver.core.factory.DatabaseFactory;
import bingo.codeweaver.core.strategy.DefaultNamingStrategy;
import bingo.codeweaver.core.strategy.NamingStrategy;

/**
 * Mar 6, 2009 6:28:27 AM DataBase.java
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:
 * ChangeLog:
 */
public class DatabaseMetadata {
	
	/**
	 * 是否延迟加载表
	 */
	private boolean lazyInit = true;
	private String username;
	private String password;
	private String url;
	private Vendor vendor;
	private String productName;
	private String productVersion;
	private String driverVersion;
	private String driver;
	private String tablespace;
	private NamingStrategy namingStrategy;
	private DatabaseFactory factory;
	
	private List<TableMetadata> tables = new ArrayList<TableMetadata>();
	
	
	public List<TableMetadata> getTables() {
		return tables;
	}

	public void setTables(List<TableMetadata> tables) {
		this.tables = tables;
	}

	public DatabaseMetadata() {
		
	}
	
	
	public DatabaseMetadata(String username, String password, String url, String driver) {
		this.username = username;
		this.password = password;
		this.url = url;
		this.driver = driver;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Vendor getVendor() {
		return vendor;
	}
	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductVersion() {
		return productVersion;
	}

	public void setProductVersion(String productVersion) {
		this.productVersion = productVersion;
	}


	public String getDriverVersion() {
		return driverVersion;
	}

	public void setDriverVersion(String driverVersion) {
		this.driverVersion = driverVersion;
	}
	
	
	
	public static enum Vendor{
		DEFAULT,
		
		DB2,
		
		HSQL,
		
		INFORMIX,
		
		MYSQL,
		
		ORACLE,
		
		POSTGRESQL, 
		
		SQL_SERVER,
		
		SYBASE
	}



	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}
	
	
	public DatabaseMetadata addTable(TableMetadata t){
		this.tables.add(t);
		return this;
	}
	
	public TableMetadata getTable(String tableName){
		if(lazyInit){
			factory.buildRequiredOracleDatabase(this, tableName);
		}
		for(TableMetadata table : this.tables){
			if(tableName.equals(table.getName())){
				return table;
			}
		}
		return null;
	}
	
	
	public String toString(){
		return this.productName;
	}

	public String getTablespace() {
		return tablespace;
	}

	public void setTablespace(String tablespace) {
		this.tablespace = tablespace;
	}

	public NamingStrategy getNamingStrategy() {
		if(namingStrategy == null){
			namingStrategy = new DefaultNamingStrategy();
		}
		return namingStrategy;
	}

	public void setNamingStrategy(NamingStrategy namingStrategy) {
		this.namingStrategy = namingStrategy;
	}

	public boolean isLazyInit() {
		return lazyInit;
	}

	public void setLazyInit(boolean lazyInit) {
		this.lazyInit = lazyInit;
	}

	public void setFactory(DatabaseFactory factory) {
		this.factory = factory;
	}
	
}
